package cn.smile.business.notebook.impl;

import cn.smile.bean.dto.core.SysUserDTO;
import cn.smile.bean.entity.notebook.NotebookShare;
import cn.smile.bean.form.core.user.QueryUserForm;
import cn.smile.business.core.ISysUserService;
import cn.smile.business.notebook.INotebookShareService;
import cn.smile.commons.domain.BaseServiceImpl;
import cn.smile.repository.notebook.NotebookShareMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class NotebookShareServiceImpl extends BaseServiceImpl<NotebookShareMapper, NotebookShare> implements INotebookShareService {
    @Resource
    private ISysUserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveShareList(List<String> shareList, String notebookId) {
        log.info("[NotebookShareServiceImpl].[saveShareList] ------> 保存共享关系集合 Start");
        //先删除旧数据
        List<NotebookShare> oldList = baseMapper.selectList(new QueryWrapper<NotebookShare>().lambda().eq(NotebookShare::getNotebookId, notebookId).eq(NotebookShare::getDeleted, Boolean.FALSE));
        if (!CollectionUtils.isEmpty(oldList)) {
            oldList.forEach(share -> super.delById(share.getId()));
        }
        if (!CollectionUtils.isEmpty(shareList)) {
            for (String shareUserId : shareList) {
                NotebookShare entity = new NotebookShare();
                entity.setNotebookId(notebookId);
                entity.setShareUserId(shareUserId);
                super.create(entity);
            }
        }
        log.info("[NotebookShareServiceImpl].[saveShareList] ------> 保存共享关系集合 End");
    }

    @Override
    public Map<String, List<SysUserDTO>> getShareMap(List<String> notebookIds) {
        log.info("[NotebookShareServiceImpl].[getShareMap] ------> 根据日记ID集合查询共享用户ID集合 Start");
        Map<String, List<SysUserDTO>> shareMap = Maps.newHashMap();
        if (!CollectionUtils.isEmpty(notebookIds)) {
            List<NotebookShare> shareList = baseMapper.selectList(new QueryWrapper<NotebookShare>().lambda().eq(NotebookShare::getDeleted, Boolean.FALSE).in(NotebookShare::getNotebookId, notebookIds));
            if (!CollectionUtils.isEmpty(shareList)) {
                Set<String> userIds = Sets.newHashSet();
                shareList.forEach(share -> userIds.add(share.getShareUserId()));
                QueryUserForm userForm = new QueryUserForm();
                userForm.setIds(Lists.newArrayList(userIds));
                Map<String, SysUserDTO> userMap = Maps.newHashMap();
                List<SysUserDTO> userList = userService.queryList(userForm);
                if (!CollectionUtils.isEmpty(userList)) {
                    userList.forEach(user -> userMap.put(user.getId(), user));
                }
                for (NotebookShare share : shareList) {
                    if (!shareMap.containsKey(share.getNotebookId())) {
                        shareMap.put(share.getNotebookId(), Lists.newArrayList());
                    }
                    if (userMap.containsKey(share.getShareUserId())) {
                        shareMap.get(share.getNotebookId()).add(userMap.get(share.getShareUserId()));
                    }
                }
            }
        }
        log.info("[NotebookShareServiceImpl].[getShareMap] ------> 根据日记ID集合查询共享用户ID集合 End");
        return shareMap;
    }

    @Override
    public List<String> getShareByUserId(String userId) {
        log.info("[NotebookShareServiceImpl].[getShareMy] ------> 查询全部分享给我的日记ID集合 Start");
        List<String> notebookIds = Lists.newArrayList();
        List<NotebookShare> shareList = baseMapper.selectList(new QueryWrapper<NotebookShare>().lambda().eq(NotebookShare::getDeleted, Boolean.FALSE).eq(NotebookShare::getShareUserId, userId));
        if (!CollectionUtils.isEmpty(shareList)) {
            shareList.forEach(share -> notebookIds.add(share.getNotebookId()));
        }
        log.info("[NotebookShareServiceImpl].[getShareMy] ------> 查询全部分享给我的日记ID集合 End");
        return notebookIds;
    }
}
